global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A12_predicting_weights.log, replace name(tabl)

capture noi {

    use ${final_dir}/regression_dataset_predictingweights_tfacit1.dta, clear
    qui do ${code_dir}/config/tabletools.do

    distinct BvD
    local nr_firms "{\num{`r(ndistinct)'}}"
    di "`nr_firms'"

    estimates clear
    
    * Run the regressions
    * 1) lsw 
    reghdfe weight g_lsw_5yr_1995, noabsorb vce(cluster country_id) 
    estadd local weighted "\no"
    estadd local firms "`nr_firms'"
    estadd local obs "{\num{`e(N)'}}"
    estimates store col_1 

    * 2) lsw + hsw 
    reghdfe weight g_lsw_5yr_1995 g_hsw_5yr_1995, noabsorb vce(cluster country_id) 
    estadd local weighted "\no"
    estadd local firms "`nr_firms'"
    estadd local obs "{\num{`e(N)'}}"
    estimates store col_2

    * 3) lsw + hsw; obs weighted with #auto95 1997-2011.
    reghdfe weight g_lsw_5yr_1995 g_hsw_5yr_1995 [aw=total_auto95_bia_1995], vce(cluster country_id) noabsorb
    estadd local weighted "\yes" 
    estadd local firms "`nr_firms'"
    estadd local obs "{\num{`e(N)'}}"
    estimates store col_3

    * 4) lsw - foreign
    reghdfe weight_foreign g_lsw_5yr_1995, noabsorb vce(cluster country_id) 
    estadd local weighted "\no"
    estadd local firms "`nr_firms'"
    estadd local obs "{\num{`e(N)'}}"
    estimates store col_4 

    * 5) lsw + hsw - foreign
    reghdfe weight_foreign g_lsw_5yr_1995 g_hsw_5yr_1995, noabsorb vce(cluster country_id) 
    estadd local weighted "\no"
    estadd local firms "`nr_firms'"
    estadd local obs "{\num{`e(N)'}}"
    estimates store col_5

    * 6) lsw + hsw; obs weighted with #auto95 1997-2011 - foreign
    reghdfe weight_foreign g_lsw_5yr_1995 g_hsw_5yr_1995 [aw=total_auto95_bia_1995], vce(cluster country_id) noabsorb
    estadd local weighted "\yes" 
    estadd local firms "`nr_firms'"
    estadd local obs "{\num{`e(N)'}}"
    estimates store col_6


    setlabels
    dhoztab * using ${tab_dir}/appendix/Table_A12_predicting_weights.tex,  b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines nomtitles sfmt(a1) scalars("weighted \noalign{\medskip} Patent weighted" "obs Observations"  "firms Firms") label drop(_cons) noobs substitute("\_" "_") tex ///
    depvar("none") mgroups("Weight" "Foreign weight", pattern(1 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) /// 
    notes("This table shows OLS regressions of firm-level weights on country growth rates for low-skill and high-skill wages between 1995 and 2000. Columns 3 and 6 weigh observations by the number of auto95 patents between 1997 and 2011. In columns 4--6, the dependent variable is the the foreign weight component only. Standard errors are clustered at the country-level.")

    *again, } brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}